package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.mapper.UserMapper;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import lombok.val;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IUserService;
import com.example.mybatisplusdemo.model.domain.User;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-06-17
 * @version v1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
//接受请求
    //发出响应
    private final Logger logger = LoggerFactory.getLogger( UserController.class );

    @Autowired
    private IUserService userService;


    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse<User> getById(@PathVariable("id") Long id)throws Exception {
        User user = userService.getById(id);
        return JsonResponse.success(user);
    }
    //协议加IP加端口
    //http://127.0.0.1:8080/api/user/suibianxie?id=1
    //针对get请求
    //参数完全匹配键名
    //用对象接，前端键名匹配对象属性名
    //传递的参数一个对象接不住，创建一个可以接住的dto对象
    @GetMapping("suibianxie")
    public JsonResponse getUserById()throws Exception {
        Long count= userService.count();
        return JsonResponse.success(count);
    }

    //post请求，传JSON数据，一定要用JSON接
    //对象必须加json转换注解
    @PostMapping("test")
    public JsonResponse xxx(@RequestBody User user)throws Exception{
        User user1 = userService.getById(user.getId());
        return JsonResponse.success(user1);
    }

    //login
    @PostMapping("login")
    public JsonResponse login(@RequestBody User user)throws Exception{
        User login = userService.login(user);
        return JsonResponse.success(login);
    }
    //yunxuduogecanshu
    @GetMapping("pageList")
    public JsonResponse selectPageList(User user, PageDTO pageDTO)throws Exception {
        Page<User> page = userService.pageList(user,pageDTO);
        return JsonResponse.success(page);
    }

    //tianjia
    @PostMapping("save")
    public JsonResponse save(@RequestBody User user)throws Exception{
        //yanzhengshifoucunzai

        boolean save = userService.save(user);
        return JsonResponse.success(save);
    }
}

